﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IceDog.SSHConnection.SharpSSH.Client;
using IceDog.SSHConnection.SharpSSH.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace IceDog.SSHConnection.SharpSSH.Pages
{
    public class SharpSSHModel : PageModel
    {
        private readonly ILogger<SharpSSHModel> _logger;
        private SharpSSHClient _client;
        public SharpSSHModel(ILogger<SharpSSHModel> logger)
        {
            _logger = logger;
        }

        //此特性表示此属性是用于和页面进行绑定的属性
        [BindProperty]
        public SSHClientInfo SSHClientInfo { get; set; }

        public void OnGet()
        {
            SSHClientInfo = new SSHClientInfo()
            {
                IP = "192.168.0.124",
                Port = 22,
                UserName = "root",
                Password = "edoc2@love00"//密码字段无法被赋值，只能在页面用value=的形式赋值
            };
            _logger.LogInformation("call: OnGet");
        }

        public void OnPostConnecting()
        {
            try
            {
                _client = new SharpSSHClient(SSHClientInfo.IP, SSHClientInfo.UserName, SSHClientInfo.Password);
                SSHClientInfo.Response = "连接成功";
            }
            catch (Exception ex)
            {
                SSHClientInfo.Response = "连接失败，错误：" + ex.Message;
            }
        }

        public void OnPostExecute()
        {
            string response = _client.Execute(SSHClientInfo.Command);
            SSHClientInfo.Response = response;
        }

        //同步和异步只能二选一

        //public Task OnGetAsync()
        //{
        //    Console.WriteLine("call: OnGetAsync");
        //    return Task.CompletedTask;
        //}

        //public Task<IActionResult> OnPostAsync()
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        return Page();
        //    }
        //    Console.WriteLine("call: OnGetAsync");
        //    _context.Customers.Add(Customer);
        //    await _context.SaveChangesAsync();

        //    return RedirectToPage("./Index");
        //}
    }
}